<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/4/26
 * Time: 11:59
 */

namespace app\index\controller;


use think\Controller;
use app\index\controller\BootstrapZs7y;

class Search extends GetSlides
{
    public function search()
    {
        $this->get_slider();//返回banner
        $A = new Api();
        if (input('key') == '') {
            $data = [
                'list' => '搜索无结果',
                'count' => 0,
                'key' => input('key'),
                'bootstrap'=>''//搜索关键字
            ];
        } else {
            $data = $A->search(input('key'));
            $page = input('get.page');//分页
            $listRow = 10;

            if (isset($page) && null !== $page && $page * $listRow <= count($data['rows'])) {
                $currentPage = $page; // 当前页
            } else if ($page * $listRow > count($data['rows'])) {
                $currentPage = ceil(count($data['rows']) / $listRow);
            } else {
                $currentPage = 1;
            }
            $bootstrap = BootstrapZs7y::make($data, $listRow, $currentPage, count($data['rows']), false, ['path' => url('Search/search'), 'query' => ['key' => input('key')]]);
            $data['rows'] = array_slice($data['rows'], ($currentPage - 1) * $bootstrap->listRows(), $bootstrap->listRows());
            foreach ($data['rows'] as $kk => $vv) { //判断是否外链文章
                if ($data['rows'][$kk]['bundle'] == 'article') {
                    if ($data['rows'][$kk]['data']['field_external_url'] != null) {
                        $data['rows'][$kk]['external_url'] = '_blank';
                    } else {
                        $data['rows'][$kk]['external_url'] = '';
                    }
                } else {
                    $data['rows'][$kk]['external_url'] = '';
                }
            }
            $data = [
                'list' => $data['rows'],
                'count' => $data['count'],
                'key' => input('key'),
                'bootstrap' => $bootstrap->render(),//搜索关键字
            ];
        }
        $this->assign("data", $data);
        $this->assign("empty",'<div class="empty-result">无数据</div>');
        $this->assign("web_title","搜索");
        return view();
    }
    public function cdx()
    {
        $this->redirect('/');   //重定向

    }

    public function _empty()
    {
        $this->redirect('/');   //空操作 解决用户恶意的输入 报错影响交互
    }
}